﻿using Coldairarrow.Entity.BackStage.Abstracts;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.BackStage.Abstracts
{
    public class BS_AbstractsBusiness : BaseBusiness<BS_Abstracts>, IBS_AbstractsBusiness, ITransientDependency
    {
        readonly Operator _operator;
        public BS_AbstractsBusiness(IDbAccessor db,Operator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<BS_Abstracts>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<BS_Abstracts>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<BS_Abstracts, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<BS_Abstracts> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public BS_Abstracts GetZCXYDataAsync()
        {
            BS_Abstracts data = null;
            var sList = GetIQueryable().Where(x => x.Type == 0).ToList();
            if (sList.Count > 0)
            {
                data = sList[0];
            }

            return data;
        }

        public async Task AddOrUpdateZCXYData(BS_Abstracts data)
        {
            var sList = GetIQueryable().Where(x => x.Type == 0).ToList();
            if (data.Id.IsNullOrEmpty())
            {
                if (sList.Count == 0)
                {
                    data.Id = IdHelper.GetId();
                    data.Type = 0;
                    data.CreateTime = DateTime.Now;
                    data.Deleted = false;
                    data.CreatorId = _operator.UserId;

                    await InsertAsync(data);
                }
                else
                {
                    var model = sList[0];
                    model.Title = data.Title;
                    model.Note = data.Note;
                    model.Contents = data.Contents;

                    await UpdateAsync(model);
                }
            }
            else
            {
                var model = GetEntity(data.Id);
                model.Title = data.Title;
                model.Note = data.Note;
                model.Contents = data.Contents;

                await UpdateAsync(model);
            }
        }

        public BS_Abstracts GetJFGZDataAsync()
        {
            BS_Abstracts data = null;
            var sList = GetIQueryable().Where(x => x.Type == 1).ToList();
            if (sList.Count > 0)
            {
                data = sList[0];
            }

            return data;
        }

        public async Task AddOrUpdateJFGZData(BS_Abstracts data)
        {
            var sList = GetIQueryable().Where(x => x.Type == 1).ToList();
            if (data.Id.IsNullOrEmpty())
            {
                if (sList.Count == 0)
                {
                    data.Id = IdHelper.GetId();
                    data.Type = 1;
                    data.CreateTime = DateTime.Now;
                    data.Deleted = false;
                    data.CreatorId = _operator.UserId;

                    await InsertAsync(data);
                }
                else
                {
                    var model = sList[0];
                    model.Title = data.Title;
                    model.Note = data.Note;
                    model.Contents = data.Contents;

                    await UpdateAsync(model);
                }
            }
            else
            {
                var model = GetEntity(data.Id);
                model.Title = data.Title;
                model.Note = data.Note;
                model.Contents = data.Contents;

                await UpdateAsync(model);
            }
        }


        public BS_Abstracts GetSYSMDataAsync()
        {
            BS_Abstracts data = null;
            var sList = GetIQueryable().Where(x => x.Type == 2).ToList();
            if (sList.Count > 0)
            {
                data = sList[0];
            }

            return data;
        }

        public async Task AddOrUpdateSYSMData(BS_Abstracts data)
        {
            var sList = GetIQueryable().Where(x => x.Type == 2).ToList();
            if (data.Id.IsNullOrEmpty())
            {
                if (sList.Count == 0)
                {
                    data.Id = IdHelper.GetId();
                    data.Type = 2;
                    data.CreateTime = DateTime.Now;
                    data.Deleted = false;
                    data.CreatorId = _operator.UserId;

                    await InsertAsync(data);
                }
                else
                {
                    var model = sList[0];
                    model.Title = data.Title;
                    model.Note = data.Note;
                    model.Contents = data.Contents;

                    await UpdateAsync(model);
                }
            }
            else
            {
                var model = GetEntity(data.Id);
                model.Title = data.Title;
                model.Note = data.Note;
                model.Contents = data.Contents;

                await UpdateAsync(model);
            }
        }

        public BS_Abstracts GetTXSMDataAsync()
        {
            BS_Abstracts data = null;
            var sList = GetIQueryable().Where(x => x.Type == 3).ToList();
            if (sList.Count > 0)
            {
                data = sList[0];
            }

            return data;
        }

        public async Task AddOrUpdateTXSMData(BS_Abstracts data)
        {
            var sList = GetIQueryable().Where(x => x.Type == 3).ToList();
            if (data.Id.IsNullOrEmpty())
            {
                if (sList.Count == 0)
                {
                    data.Id = IdHelper.GetId();
                    data.Type = 3;
                    data.CreateTime = DateTime.Now;
                    data.Deleted = false;
                    data.CreatorId = _operator.UserId;

                    await InsertAsync(data);
                }
                else
                {
                    var model = sList[0];
                    model.Title = data.Title;
                    model.Note = data.Note;
                    model.Contents = data.Contents;

                    await UpdateAsync(model);
                }
            }
            else
            {
                var model = GetEntity(data.Id);
                model.Title = data.Title;
                model.Note = data.Note;
                model.Contents = data.Contents;

                await UpdateAsync(model);
            }
        }

        public async Task AddDataAsync(BS_Abstracts data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(BS_Abstracts data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}